package com.unicom.scg.scancodego.controller;

import com.unicom.scg.scancodego.entity.TStaff;
import com.unicom.scg.scancodego.mapper.TStaffMapper;
import com.unicom.scg.scancodego.service.TstaffService;
import com.unicom.scg.scancodego.service.impl.TstaffServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * ClassName: LoginController <br/>
 * Description: <br/>
 * Date: 2019/10/26 10:25<br/>
 *
 * @author jason<br />
 * @version v1.0
 */
@Controller
public class LoginController {

    @Autowired
    TstaffService tstaffService;

    @RequestMapping("/login")
    public String login(Map<String, Object> map){
        map.put("msg","欢迎您,请登录！");
        return "login";
    }
    @RequestMapping("/")
    public String index(){
        //map.put("msg","欢迎你,请登录！");
        return "index";
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST)
//    @GetMapping
//    @PostMapping
    public String login(@RequestParam("staffId") String name,
                      @RequestParam("password") String userpwd,
                      Map<String, Object> map,
                      HttpSession session){

        System.out.println("============进行登录判断===========");

        TStaff tStaff = tstaffService.selectById(name);


        if (name.equals(tStaff.getStaffId()) && userpwd.equals(tStaff.getPassword())){
                // 登录成功
            // 将登录的状态保存到session中
            session.setAttribute("staffId",name);
            //map.put("msg","登录成功！");
            return "index"; // 视图名 view
            // redirect：重定向到某个页面的路径，可以防止表单重复提交
           // return "redirect:/index";
        } else {
                // 登录失败
            System.out.println("============登录失败===========");
           // map.put("msg","用户名密码错误，登录失败！");
            //return "redirect:/login";
            return "login";
        }


    }



}
